# Copyright 2022 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("//pw_bloat:pw_cc_size_binary.bzl", "pw_cc_size_binary")
load("//pw_bloat:pw_size_diff.bzl", "pw_size_diff")
load("//pw_bloat:pw_size_table.bzl", "pw_size_table")

package(
    default_visibility = ["//visibility:public"],
    features = ["-layering_check"],
)

licenses(["notice"])

pw_cc_size_binary(
    name = "decoder_partial_target",
    srcs = [
        "decoder_partial.cc",
    ],
    deps = [
        "//pw_bloat:bloat_this_binary",
        "//pw_preprocessor",
        "//pw_protobuf",
    ],
)

pw_size_diff(
    name = "decoder_partial_diff",
    base = "//pw_bloat:bloat_base",
    label = "Full wire-format pw_protobuf decoder",
    target = ":decoder_partial_target",
)

pw_size_table(
    name = "decoder_partial",
    reports = [
        ":decoder_partial_diff",
    ],
)

pw_cc_size_binary(
    name = "decoder_incremental_target",
    srcs = [
        "decoder_incremental.cc",
    ],
    deps = [
        "//pw_bloat:bloat_this_binary",
        "//pw_preprocessor",
        "//pw_protobuf",
    ],
)

pw_size_diff(
    name = "decoder_incremental_diff",
    base = ":decoder_partial_target",
    label = "Incremental cost of decoding additional fields",
    target = ":decoder_incremental_target",
)

pw_size_table(
    name = "decoder_incremental",
    reports = [
        ":decoder_incremental_diff",
    ],
)

cc_library(
    name = "proto_bloat",
    srcs = [
        "proto_bloat.cc",
    ],
    hdrs = ["proto_bloat.h"],
    features = ["-conversion_warnings"],
    implementation_deps = ["//pw_containers:vector"],
    deps = [
        "//pw_preprocessor",
        "//pw_protobuf",
        "//pw_status",
        "//pw_stream",
    ],
)

pw_cc_size_binary(
    name = "proto_base",
    srcs = [
        "proto_base.cc",
    ],
    deps = [
        ":proto_bloat",
        "//pw_bloat:bloat_this_binary",
    ],
)

pw_cc_size_binary(
    name = "encode_decode_core",
    srcs = [
        "encode_decode_core.cc",
    ],
    deps = [
        ":proto_bloat",
        "//pw_bloat:bloat_this_binary",
    ],
)

pw_cc_size_binary(
    name = "message_core",
    srcs = [
        "message_core.cc",
    ],
    deps = [
        ":proto_bloat",
        "//pw_bloat:bloat_this_binary",
    ],
)

pw_cc_size_binary(
    name = "messages_no_codegen",
    srcs = ["simple_codegen_comparison.cc"],
    defines = ["_PW_PROTOBUF_SIZE_REPORT_NO_CODEGEN=1"],
    deps = [
        ":proto_bloat",
        "//pw_bloat:bloat_this_binary",
        "//pw_protobuf:codegen_test_proto_pwpb",
    ],
)

pw_cc_size_binary(
    name = "messages_wire_format",
    srcs = ["simple_codegen_comparison.cc"],
    defines = ["_PW_PROTOBUF_SIZE_REPORT_WIRE_FORMAT=1"],
    deps = [
        ":proto_bloat",
        "//pw_bloat:bloat_this_binary",
        "//pw_protobuf:codegen_test_proto_pwpb",
    ],
)

pw_cc_size_binary(
    name = "messages_message",
    srcs = ["simple_codegen_comparison.cc"],
    defines = ["_PW_PROTOBUF_SIZE_REPORT_MESSAGE=1"],
    deps = [
        ":proto_bloat",
        "//pw_bloat:bloat_this_binary",
        "//pw_protobuf:codegen_test_proto_pwpb",
    ],
)

pw_cc_size_binary(
    name = "oneof_no_codegen",
    srcs = ["oneof_codegen_comparison.cc"],
    defines = ["_PW_PROTOBUF_SIZE_REPORT_NO_CODEGEN=1"],
    deps = [
        ":proto_bloat",
        "//pw_bloat:bloat_this_binary",
        "//pw_protobuf:codegen_test_proto_pwpb",
    ],
)

pw_cc_size_binary(
    name = "oneof_wire_format",
    srcs = ["oneof_codegen_comparison.cc"],
    defines = ["_PW_PROTOBUF_SIZE_REPORT_WIRE_FORMAT=1"],
    deps = [
        ":proto_bloat",
        "//pw_bloat:bloat_this_binary",
        "//pw_protobuf:codegen_test_proto_pwpb",
    ],
)

pw_cc_size_binary(
    name = "oneof_message",
    srcs = ["oneof_codegen_comparison.cc"],
    defines = ["_PW_PROTOBUF_SIZE_REPORT_MESSAGE=1"],
    deps = [
        ":proto_bloat",
        "//pw_bloat:bloat_this_binary",
        "//pw_protobuf:codegen_test_proto_pwpb",
    ],
)

pw_size_diff(
    name = "protobuf_overview_encode_decode_core",
    base = ":proto_base",
    label = "Full wire-format proto encode/decode library",
    source_filter = "pw::protobuf::*|section .code",
    target = ":encode_decode_core",
)

pw_size_diff(
    name = "protobuf_overview_message_core",
    base = ":proto_base",
    label = "Including table-based `Message` encoder and decoder",
    source_filter = "pw::protobuf::*|section .code",
    target = ":message_core",
)

pw_size_table(
    name = "protobuf_overview",
    reports = [
        ":protobuf_overview_encode_decode_core",
        ":protobuf_overview_message_core",
    ],
)

_PROTOBUF_SOURCE_FILTER = "pw::protobuf_size_report::*"

pw_size_diff(
    name = "simple_codegen_size_direct_wire_encoder",
    base = ":message_core",
    label = "Direct wire format proto encoder",
    source_filter = _PROTOBUF_SOURCE_FILTER,
    target = ":messages_no_codegen",
)

pw_size_diff(
    name = "simple_codegen_size_wrapped_wire_encoder",
    base = ":message_core",
    label = "Generated wrappers for wire format encoder",
    source_filter = _PROTOBUF_SOURCE_FILTER,
    target = ":messages_wire_format",
)

pw_size_diff(
    name = "simple_codegen_size_message_encoder",
    base = ":message_core",
    label = "Generated message encoder",
    source_filter = _PROTOBUF_SOURCE_FILTER,
    target = ":messages_message",
)

pw_size_table(
    name = "simple_codegen_size_comparison",
    reports = [
        ":simple_codegen_size_direct_wire_encoder",
        ":simple_codegen_size_wrapped_wire_encoder",
        ":simple_codegen_size_message_encoder",
    ],
)

pw_size_diff(
    name = "oneof_codegen_size_direct_wire_encoder",
    base = ":message_core",
    label = "Direct wire format proto encoder",
    source_filter = _PROTOBUF_SOURCE_FILTER,
    target = ":oneof_no_codegen",
)

pw_size_diff(
    name = "oneof_codegen_size_wrapped_wire_encoder",
    base = ":message_core",
    label = "Generated wrappers for wire format encoder",
    source_filter = _PROTOBUF_SOURCE_FILTER,
    target = ":oneof_wire_format",
)

pw_size_diff(
    name = "oneof_codegen_size_message_encoder",
    base = ":message_core",
    label = "Generated message encoder",
    source_filter = _PROTOBUF_SOURCE_FILTER,
    target = ":oneof_message",
)

pw_size_table(
    name = "oneof_codegen_size_comparison",
    reports = [
        ":oneof_codegen_size_direct_wire_encoder",
        ":oneof_codegen_size_wrapped_wire_encoder",
        ":oneof_codegen_size_message_encoder",
    ],
)
